class TrieNode {  
    constructor() {  
        this.children = {};  
        this.isEndOfWord = false;  
    }  
  
    insert(word, index = 0) {  
        const char = word[index];  
  
        if (!this.children[char] && char) {  
            this.children[char] = new TrieNode();  
        }  
  
        if (index === word.length) {  
            this.isEndOfWord = true;  
        } else {  
            this.children[char].insert(word, index + 1);  
        }  
    }  
  
    search(word, index = 0, currentResult = '') {  
        const char = word[index];  
  
        if (!this.children[char]) {  
            return false;  
        }  
  
        if (index === word.length - 1 && this.isEndOfWord) {  
            return true;  
        }  
  
        return this.children[char].search(word, index + 1, currentResult + char);  
    }  
  
    startsWith(prefix, index = 0, currentResult = '') {  
        const char = prefix[index];  
  
        if (!this.children[char]) {  
            return false;  
        }  
  
        if (index === prefix.length - 1) {  
            return true;  
        }  
  
        return this.children[char].startsWith(prefix, index + 1, currentResult + char);  
    }  
}

class Trie {  
    constructor() {  
        this.root = new TrieNode();  
    }  
  
    insertWord(word) {  
        this.root.insert(word);  
    }  
  
    filterSensitiveWords(text, replacement = '*') {  
        let result = '';  
        let currentNode = this.root;  
        let start = 0;  
  
        for (let i = 0; i < text.length; i++) {  
            const char = text[i];  
  
            // 如果字符在Trie树中  
            if (currentNode.children[char]) {  
                currentNode = currentNode.children[char];  
  
                // 如果到达某个单词的末尾  
                if (currentNode.isEndOfWord) {  
                    // 用替换符替换敏感词  
                    const sensitiveWordLength = i - start + 1;  
                    const sensitiveWord = text.substring(start, start + sensitiveWordLength);  
                    result += sensitiveWord.replace(/[^\s]/g, replacement);  
  
                    // 如果敏感词后不是空格，则添加一个空格来保持文本格式  
                    if (i < text.length - 1 && !/\s/.test(text[i + 1])) {  
                        result += ' ';  
                    }  
  
                    start = i + 1; // 跳过已替换的敏感词  
  
                    // 重置当前搜索路径到根节点  
                    currentNode = this.root;  
                }  
            } else {  
                // 如果字符不在Trie树中，并且之前已经记录了一个开始位置  
                // 则将开始位置到当前位置的字符添加到结果中  
                if (start !== i) {  
                    result += text.substring(start, i);  
                }  
                // 更新开始位置为当前字符  
                start = i;  
                // 重置搜索路径  
                currentNode = this.root;  
            }  
        }  
  
        // 添加剩余部分  
        if (start < text.length) {  
            result += text.substring(start);  
        }  
  
        return result;  
    }  
}  
  
// 使用示例  
const trie = new Trie();  
trie.insertWord('badword');  
trie.insertWord('another');  
console.log(JSON.stringify(trie.root)); 
const text = 'This is a badword test and another test.';  
const filteredText = trie.filterSensitiveWords(text);  
console.log(filteredText); // 输出: This is a **** test and ***** test.